package com.tom.compose.service.engin;

import com.tom.compose.model.aggregates.TreeRich;
import com.tom.compose.model.vo.EngineResult;
import com.tom.compose.model.vo.TreeNode;
import com.tom.compose.model.vo.TreeRoot;
import com.tom.compose.service.logic.LogicFilter;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 基础决策引擎功能
 *<pre>
 *     提供决策树流程的处理工程,有点像通过链路的关系(性别、年龄)在二叉树中虚招果实节点的过程。
 *     同时提供一个抽象方法,执行决策流程的方法,供外部做具体的实现。
 *</pre>
 * @author zx
 * @date 2024-10-09 20:13
 */
@Slf4j
public abstract class EngineBase extends EngineConfig implements IEngine{

    @Override
    public abstract EngineResult process(Long treeId, String userId, TreeRich treeRich, Map<String, String> decisionMatter);

    protected TreeNode engineDecisionMaker(TreeRich treeRich, Long treeId, String userId, Map<String, String> decisionMatter) {
        TreeRoot treeRoot = treeRich.getTreeRoot();
        Map<Long, TreeNode> treeNodeMap = treeRich.getTreeNodeMap();
        // 规则树根ID
        Long rootNodeId = treeRoot.getTreeRootNodeId();
        TreeNode treeNodeInfo = treeNodeMap.get(rootNodeId);
        //节点类型[NodeType]；1子叶、2果实
        while (treeNodeInfo.getNodeType().equals(1)) {
            String ruleKey = treeNodeInfo.getRuleKey();
            LogicFilter logicFilter = logicFilterMap.get(ruleKey);
            String matterValue = logicFilter.matterValue(treeId, userId, decisionMatter);
            Long nextNode = logicFilter.filter(matterValue, treeNodeInfo.getTreeNodeLinkList());
            treeNodeInfo = treeNodeMap.get(nextNode);

            System.out.println("决策树引擎=>{"+treeRoot.getTreeName()+"} " +
                    "userId：{"+userId+"} " +
                    "treeId：{"+treeId+"} " +
                    "treeNode：{"+treeNodeInfo.getTreeNodeId()+"} " +
                    "ruleKey：{"+ruleKey+"} " +
                    "matterValue：{"+matterValue+"}");
        }
        return treeNodeInfo;
    }

}
